package com.common.app.utls

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.common.app.R
import com.common.base.app.extras.dp2px
import com.common.base.utils.ViewUtils
import java.lang.ref.WeakReference

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2023/2/5
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class HeadTitleUtils(context: Context) : View.OnClickListener {
    private val weakReference: WeakReference<Context>
    private var titleTotalRL: RelativeLayout?
    var ivBackTotal: RelativeLayout?
    var ivBack: ImageView?
    var head_layout_right_img: ImageView?
    var head_layout_right_img2: ImageView?
    var head_layout_title_tv: TextView?
    var head_title_right_tv: TextView?
    var head_layout_right_action_view: View?
    var head_layout_right_action_view2: View?
    private var backActionListener: BackActionListener? = null
    private var rightActionListener: RightActionListener? = null
    private var rightActionListener2: RightActionListener2? = null
    private var rightTextActionListener: RightTextActionListener? = null
    private var ifsetBackAction = false
    private var ifsetRightAction = false
    private var ifsetRightAction2 = false
    private val ifsetRightTextAction = false
    private val mContext: Context

    init {
        mContext = context
        weakReference = WeakReference(context)
        titleTotalRL =
            (weakReference.get() as Activity?)?.findViewById(R.id.rl_layout_total)
        ivBackTotal =
            (weakReference.get() as Activity?)?.findViewById(R.id.head_title_iv_back_total)
        ivBack =
            (weakReference.get() as Activity?)?.findViewById(R.id.head_title_iv_back)
        head_layout_right_img =
            (weakReference.get() as Activity?)?.findViewById(R.id.head_title_right_img)
        head_layout_right_img2 =
            (weakReference.get() as Activity?)?.findViewById(R.id.head_title_right_img2)
        head_layout_title_tv =
            (weakReference.get() as Activity?)?.findViewById(R.id.head_title_title_tv)
        head_title_right_tv =
            (weakReference.get() as Activity?)?.findViewById(R.id.head_title_right_tv)
        head_layout_right_action_view =
            (weakReference.get() as Activity?)?.findViewById(R.id.iv_right_action)
        head_layout_right_action_view2 =
            (weakReference.get() as Activity?)?.findViewById(R.id.iv_right_action2)
        ivBackTotal?.setOnClickListener(this)
        head_layout_right_action_view?.setOnClickListener(this)
        head_layout_right_action_view2?.setOnClickListener(this)
    }

    /*
     * 设置头部标题
     * */
    fun setTitle(title: String?) {
        head_layout_title_tv?.text = title
    }

    /*
     * 设置标题栏右边文字
     * */
    fun getTitle(): TextView? {
        return head_layout_title_tv
    }

    /*
     * 设置标题栏右边文字
     * */
    fun getRightText(): TextView? {
        return head_title_right_tv
    }

    /*
     * 设置标题栏右边文字
     * */
    fun setRightText(text: String?) {
        head_title_right_tv?.text = text
        head_title_right_tv?.visibility = View.VISIBLE
        head_title_right_tv?.setOnClickListener(this)
    }

    /*
     * 设置标题栏右边文字
     * */
    fun setRightTextColor(color: Int) {
        head_title_right_tv?.setTextColor(color)
    }

    /*
     * 设置图片的参数并展示
     *
     * */
    fun setRightImgParams(dpWidth: Int, dpHeight: Int, picRes: Int) {
        val img_lp = head_layout_right_img?.layoutParams as RelativeLayout.LayoutParams
        img_lp.width = dp2px(dpWidth)
        img_lp.height = dp2px(dpHeight)
        head_layout_right_img?.layoutParams = img_lp
        head_layout_right_img?.setImageResource(picRes)
        head_layout_right_img?.visibility = View.VISIBLE
    }

    /*
     * 设置返回动作回调
     * */
    fun setBackActionCallBack(callBack: BackActionListener?) {
        backActionListener = callBack
        ifsetBackAction = true
    }

    /*
     * 设置右边图片动作回调
     * */
    fun setRightImg(drawable: Drawable?) {
        head_layout_right_img?.visibility = View.VISIBLE
        head_layout_right_img?.setImageDrawable(drawable)
    }
    /*
     * 右边图片动作回调
     * */
    fun getRightImg(): ImageView? {
       return head_layout_right_img
    }

    /*
     * 设置右边图片动作回调
     * */
    fun setRightActionCallBack(callBack: RightActionListener?) {
        head_layout_right_img?.isClickable = true
        rightActionListener = callBack
        ifsetRightAction = true
    }

    /*
     * 设置右边图片动作回调
     * */
    fun setRightImg2(drawable: Drawable?) {
        head_layout_right_img2?.visibility = View.VISIBLE
        head_layout_right_img2?.setImageDrawable(drawable)
    }

    /*
     * 设置右边图片动作回调
     * */
    fun setRightActionCallBack2(callBack: RightActionListener2?) {
        head_layout_right_img2?.isClickable = true
        rightActionListener2 = callBack
        ifsetRightAction2 = true
    }

    /*
     * 设置右边文字动作回调
     * */
    fun setRightTextActionCallBack(callBack: RightTextActionListener?) {
        rightTextActionListener = callBack
    }

    override fun onClick(v: View) {
        // 防止多次点击
        if (ViewUtils.isFastClick()) return

        when (v.id) {
            R.id.head_title_iv_back_total ->                 /*
                 * 返回键返回页面
                 * */if (ifsetBackAction) {
                backActionListener?.back(v)
            } else {
//                    ((Activity) weakReference.get()).overridePendingTransition(
//                            R.anim.activity_in_from_right
//                            , R.anim.activity_out_to_left);
                (weakReference.get() as Activity?)?.finish()
            }
            R.id.iv_right_action ->                 /*
                 * 标题栏右边按钮动作
                 * */if (ifsetRightAction) {
                rightActionListener?.rightAction(v)
            }
            R.id.iv_right_action2 ->                 /*
                 * 标题栏右边按钮动作
                 * */if (ifsetRightAction2) {
                rightActionListener2?.rightAction2(v)
            }
            R.id.head_title_right_tv -> rightTextActionListener?.rightTextAction(v)
        }
    }

    interface BackActionListener {
        fun back(v: View?)
    }

    interface RightActionListener {
        fun rightAction(v: View?)
    }

    interface RightActionListener2 {
        fun rightAction2(v: View?)
    }

    interface RightTextActionListener {
        fun rightTextAction(v: View?)
    }

    /**
     * 设置标题样式
     *
     * @param flag
     */
    fun setLineStyle(flag: Int) {
        if (titleTotalRL == null) {
            return
        }
        when (flag) {
            1 -> titleTotalRL?.background =
                ContextCompat.getDrawable(mContext, R.drawable.bottom_line_bg)
            2 -> titleTotalRL?.setBackgroundColor(
                ContextCompat.getColor(
                    mContext,
                    R.color.main_foreground
                )
            )
            3 -> setTrColorStyle()//透明背景白色字体
            4 -> setProductColorStyle()
            5 -> {
                titleTotalRL?.background =
                    ContextCompat.getDrawable(mContext, R.drawable.bg_f21616_to_ff9292)
                setWhiteColorView()
            }
            6 -> {
                titleTotalRL?.background =
                    ContextCompat.getDrawable(mContext, R.drawable.bg_2b7fe3_to_53a7f6)
                setWhiteColorView()
            }
            7 -> {
                titleTotalRL?.background =
                    ContextCompat.getDrawable(mContext, R.drawable.bg_ff5971_to_ff6e95)
                setWhiteColorView()
            }
            8 -> {
                titleTotalRL?.setBackgroundColor(
                    ContextCompat.getColor(
                        mContext,
                        R.color.transparent
                    )
                )
                setWhiteColorView()
            }
            9 -> {
                titleTotalRL?.background =
                    ContextCompat.getDrawable(mContext, R.drawable.bg_bd8e57_to_e5ad6b)
                setWhiteColorView()
            }
            10 -> {//透明背景黑色字体
                titleTotalRL?.setBackgroundColor(
                    ContextCompat.getColor(
                        mContext,
                        R.color.transparent
                    )
                )
                setBlackColorView()
            }
            11 -> {
                titleTotalRL?.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_a83535))
                setWhiteColorView()
            }
            12 -> {
                titleTotalRL?.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_151925))
                setWhiteColorView()
            }
            else -> titleTotalRL?.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white))
        }
    }

    /**
     *
     */
    fun setProductColorStyle() {
//        titleTotalRL.setBackgroundColor(ContextCompat.getColor(mContext, R.color.product_color));
        titleTotalRL?.background =
            ContextCompat.getDrawable(mContext, R.drawable.bg_f66121_to_f6a684)
        setWhiteColorView()
    }

    /**
     *
     */
    fun setTrColorStyle() {
        titleTotalRL?.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
        setWhiteColorView()
    }

    /**
     *
     */
    fun setWhiteColorView() {
        ivBack?.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.base_back_white_icon))
        head_layout_title_tv?.setTextColor(ContextCompat.getColor(mContext, R.color.white))
        head_title_right_tv?.setTextColor(ContextCompat.getColor(mContext, R.color.white))
    }

    /**
     *
     */
    fun setBlackColorView() {
        ivBack?.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.base_pro_back_icon))
        head_layout_title_tv?.setTextColor(ContextCompat.getColor(mContext, R.color.text_content))
        head_title_right_tv?.setTextColor(ContextCompat.getColor(mContext, R.color.text_content))
    }

    /**
     * 隐藏返回键
     */
    fun setHideBack() {
        ivBack?.visibility = View.GONE
    }
}